package zd.cornermemory.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;

import java.util.ArrayList;
import java.util.List;

import zd.cornermemory.R;
import zd.cornermemory.utils.SPUtils;
import zd.cornermemory.utils.SpKey;

/**
 * 设计设置
 */
public class TimerSettingActivity extends BaseActivity implements View.OnClickListener{
    private ImageView   input_ib;
    private ImageView cj_ib;
    private ImageView cj_long;
    private ImageView show_status;
    private ImageView left_fling;
    private ImageView up_fling;
    private Spinner sound_sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer_setting);
        input_ib = findViewById(R.id.input_ib);
        sound_sp = findViewById(R.id.sound_sp);
        cj_ib = findViewById(R.id.cj_ib);
        cj_long = findViewById(R.id.cj_long);
        show_status = findViewById(R.id.show_status);
        left_fling = findViewById(R.id.left_fling);
        up_fling = findViewById(R.id.up_fling);



        String[] sounds = getResources().getStringArray(R.array.timer_s);
        List<String> cornerList = new ArrayList<>();
        for(int i=0;i<sounds.length;i++) {
            cornerList.add(sounds[i]);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,

                R.layout.spinner_checked_text, cornerList);
        sound_sp.setAdapter(adapter);


        int index = SPUtils.getInstance().getInt(SpKey.TIMER_SOUND, 0);
        sound_sp.setSelection(index);
        sound_sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                SPUtils.getInstance().put(SpKey.TIMER_SOUND,i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        initData();
        input_ib.setOnClickListener(this);
        cj_ib.setOnClickListener(this);
        cj_long.setOnClickListener(this);
        show_status.setOnClickListener(this);
        left_fling.setOnClickListener(this);
        up_fling.setOnClickListener(this);
        setBackImage();
    }

    private void initData(){
        String isInputStr = SPUtils.getInstance().getString(SpKey.IS_INPUT);
        if(TextUtils.isEmpty(isInputStr)){
            //没有设置过，用默认
            SPUtils.getInstance().put(SpKey.IS_INPUT,"0");
            isInputStr = "0";
        }
        String isShowCjStr = SPUtils.getInstance().getString(SpKey.IS_SHOW_CJ);
        if(TextUtils.isEmpty(isShowCjStr)){
            //没有设置过，用默认
            SPUtils.getInstance().put(SpKey.IS_SHOW_CJ,"1");
            isShowCjStr = "1";
        }
        String isLongStr = SPUtils.getInstance().getString(SpKey.IS_LONG);
        if(TextUtils.isEmpty(isLongStr)){
            //没有设置过，用默认
            SPUtils.getInstance().put(SpKey.IS_LONG,"0");
            isLongStr = "0";
        }

        String isLeftStr = SPUtils.getInstance().getString(SpKey.IS_LEFT);
        if(TextUtils.isEmpty(isLeftStr)){
            //没有设置过，用默认
            SPUtils.getInstance().put(SpKey.IS_LEFT,"1");
            isLeftStr = "1";
        }
        String isUpStr = SPUtils.getInstance().getString(SpKey.IS_UP);
        if(TextUtils.isEmpty(isUpStr)){
            //没有设置过，用默认
            SPUtils.getInstance().put(SpKey.IS_UP,"1");
            isUpStr = "1";
        }

        switch (isInputStr) {
            case "0":
                setSwitchOn(input_ib,false);
                break;
            case "1":
                setSwitchOn(input_ib,true);
                break;
        }

        switch (isShowCjStr) {
            case "0":
                setSwitchOn(cj_ib,false);
                break;
            case "1":
                setSwitchOn(cj_ib,true);
                break;
        }

        switch (isLongStr) {
            case "0":
                setSwitchOn(cj_long,false);
                break;
            case "1":
                setSwitchOn(cj_long,true);
                break;
        }

        switch (isLeftStr) {
            case "0":
                setSwitchOn(left_fling,false);
                break;
            case "1":
                setSwitchOn(left_fling,true);
                break;
        }
        switch (isUpStr) {
            case "0":
                setSwitchOn(up_fling,false);
                break;
            case "1":
                setSwitchOn(up_fling,true);
                break;
        }

        boolean isShowStatus = SPUtils.getInstance().getBoolean(SpKey.STATUS_IS_OPEN, false);
        if (isShowStatus) {
            setSwitchOn(show_status,true);
        }else{
            setSwitchOn(show_status,false);
        }

    }


    private void setSwitchOn(ImageView bt, boolean isOn) {
        if(isOn) bt.setImageResource(R.drawable.switch_on);
        else bt.setImageResource(R.drawable.switch_off);
    }

    @Override
    public void onClick(View view) {
        Intent broadcast = new Intent(SpKey.NOTIFY_TIMER);
        switch (view.getId()) {
            case R.id.input_ib:
                String isInputStr = SPUtils.getInstance().getString(SpKey.IS_INPUT);
                if (TextUtils.isEmpty(isInputStr)) {
                    SPUtils.getInstance().put(SpKey.IS_INPUT, "1");
                    setSwitchOn(input_ib, true);
                } else {
                    switch (isInputStr) {
                        case "0":
                            SPUtils.getInstance().put(SpKey.IS_INPUT, "1");
                            setSwitchOn(input_ib,true);
                            break;
                        case "1":
                            SPUtils.getInstance().put(SpKey.IS_INPUT, "0");
                            setSwitchOn(input_ib,false);
                            break;
                    }
                }
                sendBroadcast(broadcast);
                break;
            case R.id.cj_ib:
                String isShowCjStr = SPUtils.getInstance().getString(SpKey.IS_SHOW_CJ);
                if (TextUtils.isEmpty(isShowCjStr)) {
                    SPUtils.getInstance().put(SpKey.IS_SHOW_CJ, "1");
                    setSwitchOn(cj_ib, true);
                } else {
                    switch (isShowCjStr) {
                        case "0":
                            SPUtils.getInstance().put(SpKey.IS_SHOW_CJ, "1");
                            setSwitchOn(cj_ib,true);
                            break;
                        case "1":
                            SPUtils.getInstance().put(SpKey.IS_SHOW_CJ, "0");
                            setSwitchOn(cj_ib,false);
                            break;
                    }
                }
                sendBroadcast(broadcast);
                break;
            case R.id.cj_long:
                String isLongStr = SPUtils.getInstance().getString(SpKey.IS_LONG);
                if (TextUtils.isEmpty(isLongStr)) {
                    SPUtils.getInstance().put(SpKey.IS_LONG, "1");
                    setSwitchOn(cj_long, true);
                } else {
                    switch (isLongStr) {
                        case "0":
                            SPUtils.getInstance().put(SpKey.IS_LONG, "1");
                            setSwitchOn(cj_long,true);
                            break;
                        case "1":
                            SPUtils.getInstance().put(SpKey.IS_LONG, "0");
                            setSwitchOn(cj_long,false);
                            break;
                    }
                }
                sendBroadcast(broadcast);
                break;
            case R.id.left_fling:
                String isLeftStr = SPUtils.getInstance().getString(SpKey.IS_LEFT);
                if (TextUtils.isEmpty(isLeftStr)) {
                    SPUtils.getInstance().put(SpKey.IS_LEFT, "1");
                    setSwitchOn(left_fling, true);
                } else {
                    switch (isLeftStr) {
                        case "0":
                            SPUtils.getInstance().put(SpKey.IS_LEFT, "1");
                            setSwitchOn(left_fling,true);
                            break;
                        case "1":
                            SPUtils.getInstance().put(SpKey.IS_LEFT, "0");
                            setSwitchOn(left_fling,false);
                            break;
                    }
                }
                sendBroadcast(broadcast);
                break;
            case R.id.up_fling:
                String isUpStr = SPUtils.getInstance().getString(SpKey.IS_UP);
                if (TextUtils.isEmpty(isUpStr)) {
                    SPUtils.getInstance().put(SpKey.IS_UP, "1");
                    setSwitchOn(up_fling, true);
                } else {
                    switch (isUpStr) {
                        case "0":
                            SPUtils.getInstance().put(SpKey.IS_UP, "1");
                            setSwitchOn(up_fling,true);
                            break;
                        case "1":
                            SPUtils.getInstance().put(SpKey.IS_UP, "0");
                            setSwitchOn(up_fling,false);
                            break;
                    }
                }
                sendBroadcast(broadcast);
                break;
            case R.id.show_status:
                boolean isShow = SPUtils.getInstance().getBoolean(SpKey.STATUS_IS_OPEN);
                if (isShow) {
                    SPUtils.getInstance().put(SpKey.STATUS_IS_OPEN, false);
                    setSwitchOn(show_status, false);
                } else {
                    SPUtils.getInstance().put(SpKey.STATUS_IS_OPEN, true);
                    setSwitchOn(show_status, true);
                }
                sendBroadcast(broadcast);
                break;
        }
    }
}
